package com.city.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @Author 何平
 * @Description //TODO RedisConfig redis开启配置
 * @Date 11:28 2018/8/3
 **/
@Configuration
@EnableCaching  //开启缓存
public class RedisConfig extends CachingConfigurerSupport {

    //注入redisconnectionfactory
    @Autowired
    RedisConnectionFactory redisConnectionFactory;

    //实例化RedisTemplate对象
    @Bean
    public RedisTemplate<String,Object> functionDomainRedisTemplate(){
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
        initDomainRedisTemplate(redisTemplate,redisConnectionFactory);
        return redisTemplate;
    }

    /**
     * @Author 何平
     * @Description //TODO initDomainRedisTemplate 设置数据存入redis的序列化方式
     * @Date 11:36 2018/8/3
     * @Param [redisTemplate, factory]
     * @return void
     **/
    private void initDomainRedisTemplate(RedisTemplate<String,Object> redisTemplate,RedisConnectionFactory factory){
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setConnectionFactory(factory);
    }

    /**
     * @Author 何平
     * @Description //TODO cacheManager 缓存管理器
     * @Date 11:38 2018/8/3
     * @Param [redisTemplate]
     * @return org.springframework.cache.CacheManager
     **/
    @Bean
    public CacheManager cacheManager(RedisTemplate<?,?> redisTemplate){
        CacheManager cacheManager = new RedisCacheManager(redisTemplate);
        return cacheManager;
    }

    /**
     * @Author 何平
     * @Description //TODO hashOperations 实例化HashOperations对象，可以使用Hash类型操作
     * @Date 13:40 2018/8/3
     * @Param [redisTemplate]
     * @return org.springframework.data.redis.core.HashOperations<java.lang.String,java.lang.String,java.lang.Object>
     **/
    @Bean
    public HashOperations<String,String,Object> hashOperations(RedisTemplate<String,Object> redisTemplate){
        return redisTemplate.opsForHash();
    }


    /**
     * @Author 何平
     * @Description //TODO valueOperations 实例化ValueOperations，可以使用String操作
     * @Date 13:41 2018/8/3
     * @Param [redisTemplate]
     * @return org.springframework.data.redis.core.ValueOperations<java.lang.String,java.lang.Object>
     **/
    @Bean
    public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForValue();
    }

   /**
    * @Author 何平
    * @Description //TODO listOperations 实例化ListOperations，可以使用List操作
    * @Date 13:42 2018/8/3
    * @Param [redisTemplate]
    * @return org.springframework.data.redis.core.ListOperations<java.lang.String,java.lang.Object>
    **/
    @Bean
    public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForList();
    }

   /**
    * @Author 何平
    * @Description //TODO setOperations 实例化SetOperations，可以使用Set操作
    * @Date 13:42 2018/8/3
    * @Param [redisTemplate]
    * @return org.springframework.data.redis.core.SetOperations<java.lang.String,java.lang.Object>
    **/
    @Bean
    public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForSet();
    }

   /**
    * @Author 何平
    * @Description //TODO zSetOperations 实例化Zset对象，可以使用Zset操作
    * @Date 13:55 2018/8/3
    * @Param [redisTemplate]
    * @return org.springframework.data.redis.core.ZSetOperations<java.lang.String,java.lang.Object>
    **/
    @Bean
    public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForZSet();
    }
}
